/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/** 
 * \file 
 * \brief Simple module to implement GIMnetAP security features - header.
 * \author Antti Maula <antti.maula@tkk.fi>
 */
#ifndef _GIMNETAP_APSECURITY_H_
#define _GIMNETAP_APSECURITY_H_

#include <inttypes.h>

#ifdef __cplusplus
extern "C" {
#endif
  
  typedef struct SAPSecurityAuthentication {
    uint8_t secretAPClient[255];
    unsigned int secretAPClientSize;

    uint8_t secretAPService[255];
    unsigned int secretAPServiceSize;

    uint8_t secretAP2AP[255];
    unsigned int secretAP2APSize;
  } TAPSecurityAuthentication;


  /** Calculate a HMAC of a data block, using given key.
   *
   * This function calculates a 160bit (20 bytes) hash value from given
   * data and given key using SHA-1 as engine.
   *
   * @param[out] hmac             Pointer to memory location which will hold
   *                              the generated hash. The memory location must 
   *                              be atleast 20 bytes in size, as this func. uses
   *                              the SHA-1, which produces 160 bits (20 bytes)
   * @param hmacsize              On call, must contain max usable size, after call will contain
   *                              size of hmac (which is 20, as currently SHA-1 is used)
   * @param[in] key               Pointer to key data
   * @param[in] keylen            Size of key in bytes
   * @param[in] data              Pointer to data
   * @param[in] datalen           Size of data in bytes
   */
  void apsecurityCalculateHMAC(uint8_t *hmac,
                               unsigned int *hmacsize,
                               const uint8_t *key,
                               const unsigned int keylen,
                               const uint8_t *data,
                               const unsigned int datalen);
  
  
  
  /** Simple utility function for reading a secret key from file.
   *
   * This function is only provided for convenience to read a number of
   * bytes from a file to application provided buffer.
   *
   * @param[out] key              Pointer to output buffer. The buffer must be
   *                              atleast the size of \p bytes.
   * @param[in] bytes             Maximum number of bytes to read from file.
   *                              If the size of file is smaller number of bytes
   *                              is read without an error.
   * @return                      Number of bytes to stored in \p key, or
   *                              negative on error. (errno set appropriately)
   *
   */
  int apsecurityReadSecretKeyFromFile(uint8_t *key, 
                                      const unsigned int bytes, 
                                      const char *filename);
  
  
  
  
  /** Read GIMnetAuth.cred fileformat, and create a
   * TAPSecurityAuthentication structure based on it.
   *
   */
  int apsecurityReadAuthenticationFile(TAPSecurityAuthentication *auth,
                                       const char *filename);
  
#ifdef __cplusplus
}
#endif
#endif


